import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:sleep_manager/ui/home/record_list_page.dart';
import 'package:sleep_manager/ui/home/setting_page.dart';
import 'monitor_page.dart';

/// 首页
class HomePage extends StatefulWidget {
  
  const HomePage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
  
}

class _HomePageState extends State<HomePage> {

  // 当前下标索引
  int _selectedIndex = 0;

  // 页面控制器
  final PageController _pageController = PageController(initialPage: 0);

  final List<Widget> _pages = const [
    MonitorPage(),
    RecordListPage(),
    SettingPage()
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView.builder(
          controller: _pageController,
          physics: const NeverScrollableScrollPhysics(), // 禁止滑动
          itemCount: _pages.length,
          itemBuilder: (BuildContext context, int index) {
            return _pages[index];
          }
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        backgroundColor: Colors.white,
        items: [
          _buildBottomNavigationBarItem("assets/images/jiance_2.svg", 'assets/images/jiance_1.svg', "监测"),
          _buildBottomNavigationBarItem("assets/images/liulanjilu_2.svg", 'assets/images/liulanjilu_1.svg', "浏览记录"),
          _buildBottomNavigationBarItem("assets/images/shezhi_2.svg", 'assets/images/shezhi_1.svg', "设置")
        ],
        currentIndex: _selectedIndex,
        selectedFontSize: 14,
        unselectedFontSize: 14,
        selectedItemColor: Colors.black,
        unselectedItemColor: Colors.black,
        onTap: _onItemClick,
      ),
    );
  }

  // 构建BottomNavigationBarItem
  BottomNavigationBarItem _buildBottomNavigationBarItem(String iconPath, String activePath, String label) {
    return BottomNavigationBarItem(
        icon: SvgPicture.asset(iconPath, width: 25, height: 25),
        activeIcon: SvgPicture.asset(activePath, width: 25, height: 25),
        label: label
    );
  }

  // bottomBar点击
  void _onItemClick(int index) {
    setState(() {
      _selectedIndex = index;
    });
    _pageController.animateToPage(
        index,
        duration: const Duration(microseconds: 300),
        curve: Curves.ease
    );
  }
}
